package com.yan.superfw_weixin_push.controller;

import com.yan.superfw_weixin_push.entity.Weather;
import com.yan.superfw_weixin_push.util.CaiHongPiUtils;
import com.yan.superfw_weixin_push.util.JiNianRiUtils;
import com.yan.superfw_weixin_push.util.WeatherUtil;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("weixin")
public class Push {

    @Value("${yan.appId}")
    private String appId;
    @Value("${yan.secret}")
    private String secret;
    @Value("${yan.user}")
    private String user;
    @Value("${yan.district_id}")
    private String districtId;
    @Value("${yan.ak}")
    private String ak;
    @Value("${yan.templateId}")
    private String templateId;
    @Value("${yan.day}")
    private String day;
    @Value("${yan.birthday}")
    private String birthday;

    @GetMapping("/push")
    public void pu(){
        this.push();
        log.info("http 测试方法成功");
    }

    public void push(){
        //1，配置
        WxMpInMemoryConfigStorage wxStorage = new WxMpInMemoryConfigStorage();
        wxStorage.setAppId(appId);
        wxStorage.setSecret(secret);
        WxMpService wxMpService = new WxMpServiceImpl();
        wxMpService.setWxMpConfigStorage(wxStorage);
        // 推送消息
//        o5m1U6hTRpoOk4g-CNiCCRBecSe0
//        o5m1U6ugnxv_oqeKNpHYMZpp9hCs
        WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
                .toUser(user)
                .templateId(templateId)
                .build();
        // 配置你的信息
        Weather weather = WeatherUtil.getWeather();
        templateMessage.addData(new WxMpTemplateData("riqi",weather.getDate() + "  "+ weather.getWeek(),"#00BFFF"));
        templateMessage.addData(new WxMpTemplateData("tianqi",weather.getText_now(),"#00FFFF"));
        templateMessage.addData(new WxMpTemplateData("low",weather.getLow() + "","#173177"));
        templateMessage.addData(new WxMpTemplateData("temp",weather.getTemp() + "","#EE212D"));
        templateMessage.addData(new WxMpTemplateData("high",weather.getHigh()+ "","#FF6347" ));
        templateMessage.addData(new WxMpTemplateData("caihongpi", CaiHongPiUtils.getCaiHongPi(),"#FF69B4"));
        templateMessage.addData(new WxMpTemplateData("lianai", JiNianRiUtils.getDay()+"","#FF1493"));
        templateMessage.addData(new WxMpTemplateData("shengri",JiNianRiUtils.getBirthday_Jo()+"","#FFA500"));
        templateMessage.addData(new WxMpTemplateData("tiangou",CaiHongPiUtils.getTianGou()+"","#FFA500"));
        templateMessage.addData(new WxMpTemplateData("zaoan",CaiHongPiUtils.getZaoan()+"","#FFA500"));
        templateMessage.addData(new WxMpTemplateData("city",weather.getCity()+"","#FFA500"));

        String beizhu = "❤";
        if(JiNianRiUtils.getLianAi() % 365 == 0){
            beizhu = "今天是恋爱" + (JiNianRiUtils.getLianAi() / 365) + "周年纪念日！";
        }
        if(JiNianRiUtils.getBirthday_Jo()  == 0){
            beizhu = "今天是生日，生日快乐呀！";
        }
        templateMessage.addData(new WxMpTemplateData("beizhu",beizhu,"#FF0000"));

        try {
            System.out.println(templateMessage.toJson());
            System.out.println(wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage));
        } catch (Exception e) {
            System.out.println("推送失败：" + e.getMessage());
            e.printStackTrace();
        }
    }

}
